package com.polaris.live.adapter.vip

import android.view.View
import com.blankj.utilcode.util.StringUtils
import com.bumptech.glide.Glide
import com.polaris.live.common.adapter.BaseBindingAdapter
import com.polaris.live.common.adapter.VBViewHolder
import com.polaris.live.common.constant.CommonConst
import com.polaris.live.common.ext.autoDp
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.databinding.ItemVipShowBinding
import com.polaris.live.im.sfs.bean.chat.ext.RouterHighlight
import com.polaris.live.popup.PreviewRichCharmPopup
import com.polaris.live.resp.back_resp.UserRichCharmLevelRewardExplainVo
import com.polaris.live.resp.back_resp.UserVipIdentity
import com.polaris.live.resp.back_resp.UserVipPrivilege
import com.polaris.live.translation.R
import com.polaris.live.utils.VipStyle
import com.polaris.live.utils.image.ImageUtils
import com.polaris.live.utils.route.RouterUtils

/**
 * VipShowAdapter  vip专属特权展示
 *
 * @author Created by 天晴 on 2024/12/10/031 11:44
 */
class VipShowAdapter(
    private val isMyVip: Boolean,
    private val level: Long,
) : BaseBindingAdapter<ItemVipShowBinding, Any>() {

    init {
        addChildClickViewIds(com.polaris.live.R.id.dressing_tv)
    }

    override fun convert(
        holder: VBViewHolder<ItemVipShowBinding>,
        item: Any,
    ) {
        holder.vb.apply {
            val vipStyle = VipStyle.getVipStyle(level)
            root.setBackgroundResource(vipStyle.vipPreviewbg)
            dressingTv.setBackgroundResource(vipStyle.vipBuyBg)
            var nameStr = ""
            var contentStr = ""
            var highlights: List<RouterHighlight>? = null
            when (item) {
                is UserVipIdentity -> {
                    nameStr = item.name
                    if (isMyVip) {
                        contentStr = item.explainMine
                        highlights = item.explainMineHighlights?.highlight
                    } else {
                        contentStr = item.explain
                        highlights = item.explainHighlights?.highlight
                    }
                    rwCl.visibility = View.VISIBLE

                    if (item.decorationType == null && !item.displayEffectUrl.isNullOrEmpty()) { //勋章
                        playImg.visibility = View.VISIBLE
                    } else if (item.decorationType != null && item.decorationType == CommonConst.DressUp.CHAT_BUBBLE) {
                        playImg.visibility = View.GONE
                    } else if (item.decorationType != null) {
                        playImg.visibility = View.VISIBLE

                    } else {
                        playImg.visibility = View.GONE
                    }

                    if ((item.width ?: 0) > 0 && (item.height ?: 0) > 0) {
                        val lp = itemImg.layoutParams
                        lp.width = item.width?.autoDp ?: 240.autoDp
                        lp.height = item.height?.autoDp ?: 166.autoDp
                        itemImg.layoutParams = lp
                    } else {
                        val lp = itemImg.layoutParams
                        lp.width = 240.autoDp
                        lp.height = 166.autoDp
                        itemImg.layoutParams = lp
                    }
                    Glide.with(context)
                        .load(ImageUtils.imgPath(item.icon))
                        .into(itemImg)
                    playImg.setOnSingleClickListener {
                        val ext = if (item.decorationType == null && !item.displayEffectUrl.isNullOrEmpty()) {
                            item.displayEffectUrl
                        } else {
                            item.ext ?: ""
                        }
                        PreviewRichCharmPopup.show(
                            context,
                            UserRichCharmLevelRewardExplainVo(
                                item.name,
                                item.icon,
                                ext,
                                "{\"width\":${260.autoDp},\"height\":${160.autoDp}}",
                                item.decorationType,
                                item.icon,
                                1,
                                item.name
                            ),
                            false
                        )
                    }
                    if (isMyVip) {
                        if (item.backpackId != null) {
                            dressingTv.visibility = View.VISIBLE
                            dressingTv.text = StringUtils.getString(R.string.wear)
                        } else {
                            dressingTv.visibility = View.GONE
                            dressingTv.text = ""
                        }
                        item.router?.router?.let {
                            if (it.contains("vipNicknameColorWear")) {
                                dressingTv.visibility = View.VISIBLE
                                dressingTv.text = StringUtils.getString(R.string.wear)
                            } else if (it.contains("vipNicknameColorUnload")) {
                                dressingTv.visibility = View.VISIBLE
                                dressingTv.text = StringUtils.getString(R.string.remove)
                            }
                        }
                    }

                }

                is UserVipPrivilege -> {
                    nameStr = item.name
                    if (isMyVip) {
                        contentStr = item.explainMine
                        highlights = item.explainMineHighlights?.highlight
                    } else {
                        contentStr = item.explain
                        highlights = item.explainHighlights?.highlight
                    }

                    rwCl.visibility = View.VISIBLE
                    playImg.visibility = View.GONE

                    val lp = itemImg.layoutParams
                    lp.width = 240.autoDp
                    lp.height = 166.autoDp
                    itemImg.layoutParams = lp

                    Glide.with(context)
                        .load(ImageUtils.imgPath(item.displayUrl))
                        .into(itemImg)
                    if (isMyVip) {
                        if (item.router?.title != null) {
                            dressingTv.visibility = View.VISIBLE
                            dressingTv.text = item.router.title
                        } else {
                            dressingTv.visibility = View.GONE
                            dressingTv.text = ""
                        }
                    }
                }
            }
            nameTv.text = nameStr
            if (highlights != null) {
                RouterUtils.handleText(
                    contentTv, contentStr,
                    highlights
                )
            } else {
                contentTv.text = contentStr
            }

        }
    }
}